package com.jy.wms.common.base;

import com.jy.wms.common.SpringUtil;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.dao.WmsStockHistoryMapper;
import com.jy.wms.pojo.WmsStockHistory;
import sun.misc.BASE64Encoder;

import java.security.MessageDigest;
import java.text.SimpleDateFormat;

/**
 * 生成摘要工具类
 */
public class DigestUtil {
    public static final String GBK="GBK";
    public static final String UTF8="UTF-8";

    /**
     * MD5
     * @param data
     * @return
     * @throws Exception
     */
    public static byte[] encryptMD5(byte[] data) throws Exception {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.update(data);
        return md5.digest();
    }

    /**
     * base64
     * @param md5
     * @return
     * @throws Exception
     */
    public static String encryptBASE64(byte[] md5) throws Exception {
        return (new BASE64Encoder()).encodeBuffer(md5);
    }

    /**
     * 摘要生成
     * @param data 请求数据
     * @param sign 签名秘钥(key或者parternID)
     * @param charset 编码格式
     * @return 摘要
     * @throws Exception
     */
    public static String digest(String data,String sign,String charset) throws Exception {
        String t = encryptBASE64(encryptMD5((data + sign).getBytes(charset)));
        // BASE64编码之后默认有一个回车换行符，这个符号在windows上 与 linux不同
        // 中通后台是按照 Windows 换行符校验的，如果调用者是linux 系统要做个转换
        if (System.getProperty("line.separator").equals("\n")) {
            String t2 = t.replaceAll("\\n", "\r\n");
           t2 = t2.replaceAll("\r\n","");
            return t2;
        } else {
            return t.replaceAll("\r\n","");
        }
    }

    /**
     * 新增库存历史信息
     * @param stockId   更改库存ID
     * @param userName  操作人
     * @param sl    操作库存数量
     * @param dxjsl 操作待下架数量(待拣货数量)
     * @param dzysl 操作待转移数量
     * @param djsl  操作冻结数量
     * @param pssl  操作破损数量
     * @param bhsl  操作待补货数量
     * @param state 模块（0收货，1上架，2.补货，3.分配，4.拣货，5.发运，6.库存转移，7.库存冻结，8.pda收货,9.pda上架，10.pda拣货，11.pda发运，12.追加分配，13.追加拣货，14.追加发运，15.快速出库）
     * @param remark 备注
     */
    public static void addStockHistory(String stockId,String userName,Integer sl,Integer dxjsl,Integer dzysl,Integer djsl,Integer pssl,Integer bhsl,Integer state,String remark){
        WmsStockHistoryMapper wmsStockHistoryMapper = SpringUtil.getBean(WmsStockHistoryMapper.class);
        WmsStockHistory wmsStockHistory = new WmsStockHistory();
        wmsStockHistory.setId(UUIDUtils.uuid());
        wmsStockHistory.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis()));
        wmsStockHistory.setStockId(stockId);
        wmsStockHistory.setUserName(userName);
        wmsStockHistory.setSl(sl);
        wmsStockHistory.setDxjsl(dxjsl);
        wmsStockHistory.setDzysl(dzysl);
        wmsStockHistory.setDjsl(djsl);
        wmsStockHistory.setPssl(pssl);
        wmsStockHistory.setBhsl(bhsl);
        wmsStockHistory.setState(state);
        wmsStockHistory.setRemark(remark);
        wmsStockHistoryMapper.baseInsertSelective(wmsStockHistory);
    }
    /**
     * sql语句内计算库存数量
     * @param stockId 库存id
     * @param sl    操作库存数量
     * @param dxjsl 操作待下架数量(待拣货数量)
     * @param dzysl 操作待转移数量
     * @param djsl  操作冻结数量
     * @param pssl  操作破损数量
     * @param bhsl  操作待补货数量
     * @param locks
     * @return
     */
    /*public static Integer updateStock(String stockId,String sl,String dxjsl,String dzysl,String djsl,String pssl,String bhsl,Integer locks){
        WmsStockHistoryMapper wmsStockHistoryMapper = SpringUtil.getBean(WmsStockHistoryMapper.class);
        WmsStock wmsStock = new WmsStock();
        wmsStock.setId(stockId);
        wmsStock.setSl(sl);
        wmsStock.setDxjsl(dxjsl);
        wmsStock.setDzysl(dzysl);
        wmsStock.setDjsl(djsl);
        wmsStock.setPssl(pssl);
        wmsStock.setBhsl(bhsl);
        wmsStock.setYdsj(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis()));
        wmsStock.setLocks(locks);
        return wmsStockHistoryMapper.updateStock(wmsStock);
    }*/
    /**
     * 直接扣减库存数量
     * @param stockId 库存id
     * @param sl    操作库存数量
     * @param dxjsl 操作待下架数量(待拣货数量)
     * @param dzysl 操作待转移数量
     * @param djsl  操作冻结数量
     * @param pssl  操作破损数量
     * @param bhsl  操作待补货数量
     * @param locks
     * @return
     */
   /* public static Integer directUpdateStock(String stockId,String sl,String dxjsl,String dzysl,String djsl,String pssl,String bhsl,Integer locks){
        WmsStockHistoryMapper wmsStockHistoryMapper = SpringUtil.getBean(WmsStockHistoryMapper.class);
        WmsStock wmsStock = new WmsStock();
        wmsStock.setId(stockId);
        wmsStock.setSl(sl);
        wmsStock.setDxjsl(dxjsl);
        wmsStock.setDzysl(dzysl);
        wmsStock.setDjsl(djsl);
        wmsStock.setPssl(pssl);
        wmsStock.setBhsl(bhsl);
        wmsStock.setYdsj(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis()));
        wmsStock.setLocks(locks);
        return wmsStockHistoryMapper.directUpdateStock(wmsStock);
    }*/
    /**
     * 调用测试
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        System.out.println(digest("中通ABC123","AAA",GBK));
    }
}